package com.paireach.bw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.paireach.bw.R;
import com.paireach.bw.adapter.Frag3Adapter;
import com.paireach.bw.base.BaseFragment;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.event.AppEvent;
import com.paireach.bw.event.HttpErrorEvent;
import com.paireach.bw.event.HttpSuccessEvent;
import com.paireach.bw.http.RequestTag;
import com.paireach.bw.http.WayBillRequest;
import com.paireach.bw.utils.JsonParse;
import com.paireach.bw.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * Created by ${cqc} on 2017/2/16.
 */

public class Frag3 extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, Frag3Adapter.OnItemClickListener {

    private static final String TAG = "Frag3";
    private View rootView;
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView recyclerView;
    private List<DriveOrder> orderList = new ArrayList<>();
    private Frag3Adapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.frag2, container, false);
        findViews();
        initViews();
        showProgressDialog();
        new WayBillRequest().completeOrderList(phoneNumber);
        return rootView;
    }

    private void findViews() {
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refreshLayout);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
    }

    private void initViews() {
        refreshLayout.setColorSchemeResources(R.color.item3, R.color.item11, R.color.gold);
        refreshLayout.setOnRefreshListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.addItemDecoration(new DividerItemDecoration(context, LinearLayoutManager.VERTICAL));
        adapter = new Frag3Adapter(orderList);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(this);
    }

    @Override
    public void onRefresh() {
        new WayBillRequest().completeOrderList(phoneNumber);
    }


    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.COMPLETE_ORDER_LIST) {
            refreshLayout.setRefreshing(false);
            JsonParse.parseCompleteOrderList(event.getJson());
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if (event.getRequestTag() == RequestTag.COMPLETE_ORDER_LIST) {
            dismissProgressDialog();
            refreshLayout.setRefreshing(false);
//            Toasty.warning(context, event.getErrorMessage()).show();
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("complete_order_list")) {
            if (event.getCode() == 0) {
                orderList.clear();
                adapter.notifyDataSetChanged();
                showToast(event.getExtraInfo());
            } else if (event.getCode() == 1) {
                List<DriveOrder> orders = (List<DriveOrder>) event.getObj1();
                orderList.clear();
                orderList.addAll(orders);
                adapter.notifyDataSetChanged();
            } else {
                showToast(event.getExtraInfo());
            }
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        LogUtil.d(TAG, "onItemClick");
    }


}
